如何在r中执行照片标题字符串匹配
我想创建一个包含三个变量的仪表板:年、月和日,并显示图像 每个年份都有两个值:20192020。月份:1月、2月日:01,02。 我有8张不同的png图片。图片名称为m01_y19_d01,表示月份:1月,年份:2019年,第1天。所以这8张不同的照片是 “m01_y19_01.png”、“m01_y19_02.png”, “m01_y20_01.png”、“m01_y20_02.png”、“m02_y19_01.png”、“m02_y19_02.png”、“m02_y20_01.png”, “m02_y20_02.png” 我想在菜单中选择年、月和日,并在www文件夹中反映相应的图片 我确实写了一些代码,但看起来效果不太好。我希望有人能提供一些好的方法 多谢各位如何在r中执行照片标题字符串匹配,r,shiny,shiny-server,shinyapps,R,Shiny,Shiny Server,Shinyapps,我想创建一个包含三个变量的仪表板:年、月和日,并显示图像 每个年份都有两个值:20192020。月份:1月、2月日:01,02。 我有8张不同的png图片。图片名称为m01_y19_d01,表示月份:1月,年份:2019年,第1天。所以这8张不同的照片是 “m01_y19_01.png”、“m01_y19_02.png”, “m01_y20_01.png”、“m01_y20_02.png”、“m02_y19_01.png”、“m02_y19_02.png”、“m02_y20_01.png”, “
library(shiny)
ui <- fluidPage(
wellPanel(
fluidRow(
column(3,
selectInput("Year", "year",c("2019"="2019","2020"="2020"
))),
column(3,
selectInput("Day", "day",c("01"="01","02"="02"
))),
column(3,
selectInput("Month", "month",c("Jan"="Jan","Feb"="Feb"
))))),
fluidRow(uiOutput(outputId ='imageR1')))
server <- function(input, output)
{
output$imageR1<- renderUI(
{
if(input$Month=="Jan")
{return(m="m01")}
else if(input$Month=="Feb")
{return(m="m02")}
else if(input$Year=="2019")
{return(m=paste0(m,"_y19"))}
else if(input$Year=="2020")
{return(m=paste0(m,"_y20"))}
else if(input$Day=="01")
{return(m=paste0(m,"_d01"))}
else if(input$Day=="02")
{return(m=paste0(m,"_d02"))}
img(src = paste0(m,".png"), height = 500, width = 500)}
}
shinyApp(ui = ui, server = server)
图书馆(闪亮)
ui将if
语句更改为如下所示:
if(input$Month=="Jan")
{m="m01"}
else if(input$Month=="Feb")
{m="m02"}
if(input$Year=="2019")
{m=paste0(m,"_y19")}
else if(input$Year=="2020")
{m=paste0(m,"_y20")}
if(input$Day=="01")
{m=paste0(m,"_d01")}
else if(input$Day=="02")
{m=paste0(m,"_d02")}
但实际上,你可以用一句话来表达:
m <- format(as.Date(paste(which(substr(month.name,1,3)%in%input$Month),input$Day,input$Year,sep="/"), format="%m/%d/%Y"),"m%m_y%y_%d")
m您可以使用lubridate
软件包首先根据您的输入创建一个date
(您也可以在Shining中使用dateInput
),然后根据您的文件名格式化日期:
format(ymd(input$Year, input$Month, input$Day), format = "m%Om_y%y_%d.png")