在 R Shiny 中根据用户输入动态创建图像和表格
我正在尝试在R Shiny中开发我的第一个应用程序,但是到目前为止,我发现的示例一直在展示如何使用专有数据集(例如carot或mtcars),而不是如何有效地将数据加载到R Shiny中进行处理。
我想做的是根据用户的输入,将r Shiny创建和加载不同的数据集,然后将该数据集显示为伴随可视化的表。
到目前为止,我尝试过的解决此问题的方法已在下面的代码块中列出。 创建和生产的是用户定义的数据预处理函数,非常长并且正常工作。
CreateSnapshot 正在撤回原始数据并将其保存为CSV。 productable 加载由创建的CSVS创建的CSV ,并创建摘要表和图形,然后将其本地保存为CSV和WMF。
当我尝试运行应用程序时,服务器中的许多线路都试图加载摘要表并渲染它不运行。我似乎也无法从用户输入生成的WMF加载r闪亮。我尝试用来渲染图像的代码已被删除,并且没有显示在下面的代码块中:
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("FirstApp"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Text for providing a caption ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
# Input: Selector for choosing dataset ----
selectInput(inputId = "dis",
label = "Choose a discipline:",
choices = c("Choice 1", "Choice 2", "Choice 3")),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: HTML table attempts ----
tableOutput("view"),
tableOutput("statstable"),
tableOutput("VisData"),
tableOutput("fileData"),
imageOutput("preImage")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
#Creating Base Dataset
Snapshot<-reactive({CreateSnapshot(input$dis)})
#Creating Summary Dataset
VisualizationTable<-reactive({ProduceTable(input$dis)})
#Attempting to create file from CSV files generated above.
fileData <- reactive({reactiveFileReader(1000, NULL, paste( input$dis, date.of.data,
"FilenameBoilerPlate.csv"), read.csv)})
VisData<-renderDataTable(read.csv(paste( input$dis, date.of.data,
"FileNameBoilerPlate.csv"), read.csv)))
#Attempting to create outputs for viewing. I have discovered that the first two allow for my user defined functions to run and create all of the files I need.
output$view <- renderTable({
head(Snapshot())
})
output$statstable <- renderTable({
head(VisualizationTable())
})
output$fileData <- renderTable({
fileData()
})
#Creating Image
output$preImage <- renderImage({
# When input$n is 3, filename is ./images/image3.jpeg
filename <- normalizePath(file.path('./images', paste(input$dis, date.of.data,
"Superbarplot.wmf", sep=' ')))
# Return a list containing the filename and alt text
list(src = filename,
alt = paste("Image Discipline", input$dis))
}, deleteFile = FALSE)
}
# Create Shiny app ----
shinyApp(ui, server)
关于如何进行的任何想法将不胜感激。
I am trying to develop my first application in R Shiny, but so far the examples that I have found have been displaying how to make use of proprietary datasets (like carot or mtcars) rather than how to effectively load data into R Shiny for processing.
What I want to do is have R shiny create and load a different dataset depending on the input of the user and then display that dataset as a table accompanied by a visualization.
The approaches I have tried so far to solve this problem are listed in the code block below. CreateSnapshot and ProduceTable are user defined data preprocessing functions that are very long and are working properly.
CreateSnapshot is pulling raw data and saving it as a CSV. ProduceTable loads the CSVs created by CreateSnapshot and creates summary tables and a graphic that it then saves locally as a CSV and a WMF respectively.
When I attempt to run the application, numerous lines in the server trying to load the summary table and render it do not run. I also cannot seem to get R Shiny to load the WMF that is being generated by user input either. The code I've tried to use to render the images has since been deleted and does not show up in the code block below :
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("FirstApp"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Text for providing a caption ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
# Input: Selector for choosing dataset ----
selectInput(inputId = "dis",
label = "Choose a discipline:",
choices = c("Choice 1", "Choice 2", "Choice 3")),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: HTML table attempts ----
tableOutput("view"),
tableOutput("statstable"),
tableOutput("VisData"),
tableOutput("fileData"),
imageOutput("preImage")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
#Creating Base Dataset
Snapshot<-reactive({CreateSnapshot(input$dis)})
#Creating Summary Dataset
VisualizationTable<-reactive({ProduceTable(input$dis)})
#Attempting to create file from CSV files generated above.
fileData <- reactive({reactiveFileReader(1000, NULL, paste( input$dis, date.of.data,
"FilenameBoilerPlate.csv"), read.csv)})
VisData<-renderDataTable(read.csv(paste( input$dis, date.of.data,
"FileNameBoilerPlate.csv"), read.csv)))
#Attempting to create outputs for viewing. I have discovered that the first two allow for my user defined functions to run and create all of the files I need.
output$view <- renderTable({
head(Snapshot())
})
output$statstable <- renderTable({
head(VisualizationTable())
})
output$fileData <- renderTable({
fileData()
})
#Creating Image
output$preImage <- renderImage({
# When input$n is 3, filename is ./images/image3.jpeg
filename <- normalizePath(file.path('./images', paste(input$dis, date.of.data,
"Superbarplot.wmf", sep=' ')))
# Return a list containing the filename and alt text
list(src = filename,
alt = paste("Image Discipline", input$dis))
}, deleteFile = FALSE)
}
# Create Shiny app ----
shinyApp(ui, server)
Any thoughts about how to proceed would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将通过将您在观察的功能中包装所有您所做的一切来解决这个问题。我认为反应性不是试图使您的功能运行的自然方法,因为它们更多地是在返回需要在需要时运行的R对象输出。
类似:
观察Event(输入$ dis,
{
快照&lt; - createSnapShot(input $ dis)
可视化&lt; - 生产(输入$ dis)
filedata&lt; - 您选择的读取功能,因为它不再需要是反应性
输出$ filedata&lt; - rendertable({{{{
菲达塔
})
ETC..
}
)
因此,每次用户选择纪律时,整个事情都会运行,生成所需的输出。
如果没有理由要产生CSV,则可以通过不保存CSV来更简化此问题,因为您将其保存仅用于读取文件以加载文件。
I would approach this by wrapping essentially all of what you're doing in an observeEvent function. I don't think reactive are the natural way to try to get your functions to run since they're more about returning an R object output that runs when required.
Something like:
observeEvent(input$dis,
{
Snapshot <- CreateSnapshot(input$dis)
VisualizationTable <- ProduceTable(input$dis)
fileData <- your read function of choice since it no longer needs to be reactive
output$fileData <- renderTable({
fileData
})
etc..
}
)
So every time the user chooses a discipline the entire thing will run, generating the outputs you want.
If there's no reason you want to produce a csv you could streamline this more by not saving the csv since you save it only to read the file to load it.