从 data.frame 中删除行(R 闪亮)
Deleting row from data.frame ( R shiny)
我正在尝试编写一个闪亮的应用程序,它可以读取您的 *.csv 文件并从该文件生成绘图。该文件有一个 header 和包含几行的底部,用户应该能够在闪亮的应用程序中删除这些行。所以基本上这个编辑过的文件就是情节的来源。
我不确定如何根据输入文件创建反应部分。从这个页面尝试了几种方法,但我无法让它工作。我附上了一个简化的 Test.csv 文件。
if (!require("shiny")) install.packages("shiny", dependencies = TRUE)
if (!require("shinyjs")) install.packages("shinyjs", dependencies = TRUE)
if (!require("DT")) install.packages("DT", dependencies = TRUE)
library(shiny)
library(shinyjs)
library(DT)
ui <- fluidPage(
tabsetPanel(
tabPanel("Upload File",
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
tags$br(),
checkboxInput('header', 'Header', FALSE),
radioButtons('sep', 'Separator', c(Semicolon=';', Comma=',', Tab='\t'), ','),
radioButtons('quote', 'Quote',c(None='', 'Double Quote'='"', 'Single Quote'="'"), '"'),
actionButton('delete_row', 'Delete row')
),
mainPanel(
DT::dataTableOutput('contents')
)
)
),
tabPanel("Plot",
pageWithSidebar(
headerPanel('Visualisation'),
sidebarPanel(
selectInput('xcol', 'X Variable', ""),
selectInput('ycol', 'Y Variable', "", selected = ""),
textOutput("m_out")
),
mainPanel(
plotOutput('MyPlot')
)
)
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
return(df)
})
### This part is the problem
###
observeEvent(input$delete_row, {
if (!is.null(input$contents_rows_selected)) {
#print(input$contents_rows_selected) #testing input
data$values <- data$values[-nrow(input$contents_rows_selected),]
}
})
###
### End of problem
output$contents = DT::renderDataTable({
data()
})
output$MyPlot <- renderPlot({
x <- data()[, c(input$xcol, input$ycol)]
plot(x)
})
}
### End of server commands
### Start Shiny App
shinyApp(ui = ui, server = server)
提前感谢您的帮助。问题标记为###
尝试将您的数据变量设为 reactiveVal()。我还建议将您的 input$file1 放入数据框中。
在服务器函数中:
data <- reactiveVal(NULL)
并将其值设置到观察提交输入文件的事件中:
observeEvent(input$file1, {
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
data(df)
})
删除该行将类似于:
...
df2 <- data()
df2 <- df2[-nrow(input$contents_rows_selected),]
data(df2)
...
这将允许您的其他 UI 函数在观察到(反应性)data() 对象的更改时触发。
我正在尝试编写一个闪亮的应用程序,它可以读取您的 *.csv 文件并从该文件生成绘图。该文件有一个 header 和包含几行的底部,用户应该能够在闪亮的应用程序中删除这些行。所以基本上这个编辑过的文件就是情节的来源。
我不确定如何根据输入文件创建反应部分。从这个页面尝试了几种方法,但我无法让它工作。我附上了一个简化的 Test.csv 文件。
if (!require("shiny")) install.packages("shiny", dependencies = TRUE)
if (!require("shinyjs")) install.packages("shinyjs", dependencies = TRUE)
if (!require("DT")) install.packages("DT", dependencies = TRUE)
library(shiny)
library(shinyjs)
library(DT)
ui <- fluidPage(
tabsetPanel(
tabPanel("Upload File",
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
tags$br(),
checkboxInput('header', 'Header', FALSE),
radioButtons('sep', 'Separator', c(Semicolon=';', Comma=',', Tab='\t'), ','),
radioButtons('quote', 'Quote',c(None='', 'Double Quote'='"', 'Single Quote'="'"), '"'),
actionButton('delete_row', 'Delete row')
),
mainPanel(
DT::dataTableOutput('contents')
)
)
),
tabPanel("Plot",
pageWithSidebar(
headerPanel('Visualisation'),
sidebarPanel(
selectInput('xcol', 'X Variable', ""),
selectInput('ycol', 'Y Variable', "", selected = ""),
textOutput("m_out")
),
mainPanel(
plotOutput('MyPlot')
)
)
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
return(df)
})
### This part is the problem
###
observeEvent(input$delete_row, {
if (!is.null(input$contents_rows_selected)) {
#print(input$contents_rows_selected) #testing input
data$values <- data$values[-nrow(input$contents_rows_selected),]
}
})
###
### End of problem
output$contents = DT::renderDataTable({
data()
})
output$MyPlot <- renderPlot({
x <- data()[, c(input$xcol, input$ycol)]
plot(x)
})
}
### End of server commands
### Start Shiny App
shinyApp(ui = ui, server = server)
提前感谢您的帮助。问题标记为###
尝试将您的数据变量设为 reactiveVal()。我还建议将您的 input$file1 放入数据框中。 在服务器函数中:
data <- reactiveVal(NULL)
并将其值设置到观察提交输入文件的事件中:
observeEvent(input$file1, {
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
updateSelectInput(session, inputId = 'xcol', label = 'X Variable', choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y Variable', choices = names(df), selected = names(df)[2])
data(df)
})
删除该行将类似于:
...
df2 <- data()
df2 <- df2[-nrow(input$contents_rows_selected),]
data(df2)
...
这将允许您的其他 UI 函数在观察到(反应性)data() 对象的更改时触发。