如何解决如何在 r Shiny
我想创建一个包含三个变量的仪表板:年、月和日并显示图像
每个都有两个值 year:2019,2020。月份:一月,二月天:01,02。
我有 8 张不同的 png 图片。图片名称是 m01_y19_d01 表示月:一月,年:2019,第 01 天。所以 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文件夹中反映相应的图片。
我确实写了一些代码,但看起来效果不佳。希望有人能提供一些好的方法。
谢谢。
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"
))),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)
解决方法
将 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")
此行将 r 常量 month.name
转换为月份名称,例如“Jan”、“Feb”,并找出与您选择的月份匹配的数字。然后结合日期和年份并转换为您在图像名称中使用的日期格式。
您可以使用 lubridate
包首先根据您的输入创建一个 date
(您也可以在闪亮中使用 dateInput
),然后根据您的文件名格式化日期:
format(ymd(input$Year,input$Month,input$Day),format = "m%Om_y%y_%d.png")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。